package cn.it.services.impl;

import cn.it.domain.Business;
import cn.it.domain.Car;
import cn.it.domain.Good;
import cn.it.mapper.BusinessMapper;
import cn.it.mapper.IGoodMapper;
import cn.it.services.IGoodServices;
import cn.it.util.MyException;
import cn.it.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class GoodServicesImpl implements IGoodServices {

    @Autowired
    private IGoodMapper iGoodMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public List<Good> queryGoodsByType(Integer typeId) throws MyException {
        if (typeId == null) {
            throw new MyException("请输入正确的id");
        } else {
            List<Good> goods = null;
            if (typeId == 0){
                goods = iGoodMapper.queryGoodAll();
                return goods;
            }
            System.out.println("查询数据库");
             goods = iGoodMapper.queryGoodsByType(typeId);
            return goods;
        }
    }

    @Override
    public Result queryGoodsById(Integer id) {
        if (id == null){
            return new Result(201,"请输入正确的ID",null);
        }else{
            Good good = iGoodMapper.queryGoodById(id);
            System.out.println("---------"+good.getBusinessId());
            Business business = null ;
            business= businessMapper.queryById(good.getBusinessId());
            if (business == null){
                System.out.println("空");
                return new Result(201,"没有查询到对应商家",null);
            }else{
                Map<String,Object> map = new HashMap<>();
                map.put("good",good);
                map.put("business",business);
                System.out.println(map.toString());
                return new Result(200,"查询成功",map);
            }
        }
    }
}
